﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;
using BaseDll;
using System.IO;
using Newtonsoft.Json;

namespace OtherDevice
{
    [Description("Form_光源控制器")]
    public partial class XNCLightCtrl : Form_ControlBase
    {
        public List<string> 通道名称集合 = new List<string>();
        public XNCLightCtrl()
        {
            InitializeComponent();
        }
        LightCtrlBase lightCtrlBase;
        public override void FlushToDlg(object devicebase)
        {
            try
            {
                if (devicebase == null)
                    return;
                this.lightCtrlBase = devicebase as LightCtrlBase;
                this.Dock = DockStyle.Fill;
                this.Show();
            }
            catch (Exception)
            {

            }

        }
        private void XNCLightCtrl_Load(object sender, EventArgs e)
        {
            cbb_ChannalList.Items.Clear();
            for (int i = 0; i < 10; i++)
            {
                cbb_ChannalList.Items.Add(i);
            }
           

            for (int i = 0; i < cbb_ChannalList.Items.Count; i++)
            {
                通道名称集合.Add($"{i}");
            }
            FlushName();
            cbb_ChannalList.SelectedIndex = 0;
        }


        private void txt_MaxLightValue_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                if (e.KeyChar == 13)
                {
                    tb_LightValue.Maximum = Convert.ToInt32(txt_MaxLightValue.Text);
                }
            }
            catch (Exception ex)
            {
                string Err = $"{ex.Message} {ex.StackTrace}";
                UIMessageBox.ShowError(Err);
            }

        }

        private void tb_LightValue_Scroll(object sender, EventArgs e)
        {
            try
            {
                int channal = Convert.ToInt32(cbb_ChannalList.SelectedIndex);
                int value = tb_LightValue.Value;
                lightCtrlBase.Set(channal, value);
                
                txt_当前值.Text = value.ToString();
                System.Threading.Thread.Sleep(50);
            }
            catch (Exception ex)
            {
                string Err = $"{ex.Message} {ex.StackTrace}";
                UIMessageBox.ShowError(Err);
            }
        }

        private void btn_CloseLight_Click(object sender, EventArgs e)
        {
            try
            {
                int channal = Convert.ToInt32(cbb_ChannalList.SelectedIndex);
                int n_Value = Convert.ToInt32(txt_当前值.Text);
                lightCtrlBase.Set(channal, n_Value);
            }
            catch (Exception ex)
            {
                string Err = $"{ex.Message} {ex.StackTrace}";
                UIMessageBox.ShowError(Err);
            }
        }

        public void SetValue(int channal,int value)
        {
            try
            {
                lightCtrlBase.Set(channal, value);
            }
            catch (Exception ex)
            {
                string Err = $"{ex.Message} {ex.StackTrace}";
                UIMessageBox.ShowError(Err);
            }
            
        }

        private void 名称修改_Click(object sender, EventArgs e)
        {
            if (cbb_ChannalList.SelectedIndex == -1)
            {
                MessageBox.Show($"请先选择通道号");
                return;
            }
            if(Ch_Name.Text != "")
            {
                通道名称集合[cbb_ChannalList.SelectedIndex] = Ch_Name.Text;
            }
            Save();
            FlushName();
        }

        private void FlushName()
        {
            List<string> data = Read();
            if(data !=null)
            {
                通道名称集合 = data;
            }
            cbb_ChannalList.Items.Clear();
            for(int i = 0; i< 通道名称集合.Count; i++)
            {
                cbb_ChannalList.Items.Add(通道名称集合[i]);
            }
        }

        public int Save()
        {
            string currentFile = AppDomain.CurrentDomain.BaseDirectory + $"\\config\\{lightCtrlBase.strName}.json";
            bool a = AccessJosnSerializer.ObjectToJson(currentFile, 通道名称集合);
            return 0;
        }
        public List<string> Read()
        {
            try
            {
                string currentFile = AppDomain.CurrentDomain.BaseDirectory + $"\\config\\{lightCtrlBase.strName}.json";
                if (!File.Exists(currentFile))
                {
                    Save();
                }
                string FileText = File.ReadAllText(currentFile);
                List<string> ds = JsonConvert.DeserializeObject<List<string>>(FileText);
         
                return ds;

            }
            catch (Exception ex)
            {
                MessageBox.Show($"加载模块数据失败，错误信息：{ex.Message}");
                return null;
            }
        }

    }
    
}
